<?php


namespace app\admin\controller;


use app\BaseController;
use app\Request;

class Store extends  BaseController
{
    public function getList(Request  $request)
    {
        $page = $request->post('page', 1);
        $limit = $request->post('limit', 10);
        $where = [];

        if(!empty($this->userInfo['username']) && $this->userInfo['username'] !='admin'){
            $where['manager_id'] = $this->userInfo['id'];
        }
        $result = \app\admin\model\Store::order('sort desc id desc')->where($where)->page($page, $limit)->select();
        $total = \app\admin\model\Store::count();
        //增加商户信息
        $result = \app\admin\model\Manager::getMananger($result);
        $data = [
            'total' => $total,
            'current' => $page,
            'perpage' => $limit,
            'data' =>  $result
        ];

        return show(config('status.success'), '操作成功', $data);
    }

    /**
     * 创建
     * @param Request $request
     * @return \think\response\Json
     */
    public function create(Request $request)
    {
        \app\admin\model\Store::create([
            'manager_id' => $this->userInfo['id']??0,
            'name' => $request->post('name'),
            'thumb_url' => $request->post('thumb_url'),
            'banners' => $request->post('banners'),
            'status' => $request->post('status'),
            'address' => $request->post('address'),
            'longitude_and_atitude' => $request->post('longitude_and_atitude'),
            'start_price' => $request->post('start_price'),
            'start_minutes' => $request->post('start_minutes'),
            'out_price' => $request->post('out_price'),

            'first_user_pay_type' => $request->post('first_user_pay_type'),
            'first_start_price' => $request->post('first_start_price'),
            'first_start_minutes' => $request->post('first_start_minutes'),
            'first_out_price' => $request->post('first_out_price'),
            'package_id' => $request->post('package_id'),
            'send_minutes' => $request->post('send_minutes'),
            'sort' => $request->post('sort'),
            'telephone' => $request->post('telephone')
        ]);
        return show(config('status.success'), '操作成功');
    }

    /**
     * 更新
     * @param Request $request
     * @return \think\response\Json409eff
     */
    public function update(Request $request)
    {
        \app\admin\model\Store::where(['id' => $request->post('id')])->update([
            'name' => $request->post('name'),
            'thumb_url' => $request->post('thumb_url'),
            'banners' => json_encode($request->post('banners')),
            'status' => $request->post('status'),
            'address' => $request->post('address'),
            'longitude_and_atitude' => $request->post('longitude_and_atitude'),
            'start_price' => $request->post('start_price'),
            'start_minutes' => $request->post('start_minutes'),
            'out_price' => $request->post('out_price'),

            'first_user_pay_type' => $request->post('first_user_pay_type'),
            'first_start_price' => $request->post('first_start_price'),
            'first_start_minutes' => $request->post('first_start_minutes'),
            'first_out_price' => $request->post('first_out_price'),
            'package_id' => $request->post('package_id'),
            'send_minutes' => $request->post('send_minutes'),
            'sort' => $request->post('sort'),
            'telephone' => $request->post('telephone')
        ]);

        return show(config('status.success'), '操作成功');
    }

    public function delete(Request  $request)
    {
        \app\admin\model\Store::destroy($request->post('id'));
        return show(config('status.success'), '操作成功');
    }

    /**
     * 获取门店列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getStoreList(){
        $where = [];
        if(!empty($this->userInfo['username']) && $this->userInfo['username'] !='admin'){
            $where[] = ['manager_id','=',$this->userInfo['id']];
        }
        $list = \app\admin\model\Store::order('sort desc id desc')->where($where)->select();
        return show(config('status.success'), '操作成功', $list);
    }

    /**
     * 是否允许添加门店
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function isAllowAdd(Request  $request){
        $where['manager_id'] = $this->userInfo['id'];
        $num = \app\admin\model\Store::where($where)->count('id');
        $manage = \app\admin\model\Manager::find($where);
        if($manage['allow_store']==0 || $num>=$manage['allow_store']){
            return show(config('status.error'), "您允许添加的门店数为{$manage['allow_store']}家,您已添加{$num}家,如想添加请联系平台。");
        }else{
            return show(config('status.success'), '操作成功');
        }
    }
}